import 'package:app_core/app_core.dart';
import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:yh_core_flutter_project/project/widgets/smart_dialog_config.dart';

import '../common/constant/index.dart';
import '../common/intl/index.dart';
import '../common/managers/theme_manager/theme_manager.dart';
import '../router/app_routers.dart';
import 'app_env_config.dart';


///应用级配置（路由、主题、调试）
class MyAppConfig extends AppConfig {
  @override
  List<GetPage>? get getPages => AppPages.pages;

  @override
  String get initRouter => AppPages.initialRoute;

  @override
  bool get isDebug => EnvManager.currentEnv != EnvType.prod;

  @override
  ThemeData get theme {
    /// 确保主题管理器已初始化
    if (Get.isRegistered<ThemeManager>()) {
      return ThemeManager.instance.currentTheme;
    }
    /// 默认返回浅色主题
    return ThemeData.light();
  }

  @override
  bool get isDarkStatusBar {
    if (Get.isRegistered<ThemeManager>()) {
      return ThemeManager.instance.isDarkMode ? false : true;
    }
    return true;
  }
}

///国际化配置（语言、翻译）
class MyIntlConfig extends IntlConfig {
  @override
  LanguageType get initLanguage => _detectSystemLanguage();

  /// 根据系统语言自动检测
  LanguageType _detectSystemLanguage() {
    final systemLocale = AppPlatform.localeName.toLowerCase();
    if (systemLocale.startsWith('zh')) return LanguageType.zh;
    return LanguageType.en; // 默认英文
  }

  @override
  List<List<IntlBase>> get intlValues => [MineIntl.values, HomeIntl.values, LoginIntl.values, AddDeviceIntl.values, DeviceIntl.values];

  @override
  List<LanguageType> get supportLanguages => [LanguageType.zh, LanguageType.en];
}

///UI组件配置（导航栏样式、颜色设置）
class MyWidgetConfig extends WidgetConfig {
  @override
  Widget get backIcon => MyAppBar.svgIcon(icon: AppIcon.icBack, onTap: () => Get.back());

  @override
  Color get indicatorColor => AppColor.black;

  @override
  Color get navBackgroundColor => AppColor.white;

  @override
  TextStyle get navTitleStyle => AppStyles.navTitleStyle;

  @override
  TextStyle get textFieldHintStyle => AppStyles.textFieldHintStyle;

  @override
  TextStyle get textFieldTextStyle => AppStyles.textFieldStyle;
}

/// 只负责 SmartDialog 的全局配置
class MyDialogConfig extends DialogConfig {

  static const LoadingType _currentLoadingType = LoadingType.spinner;

  static const ToastType _currentToastType = ToastType.smallRounded;

  @override
  Color get dialogMaskColor => AppColor.maskColor;

  @override
  Widget loadingWidget(String msg) {
    return LoadingWidgetFactory.create(_currentLoadingType, msg);
  }

  @override
  Widget toastWidget(String msg) {
    return ToastWidgetFactory.create(_currentToastType, msg, currentMsgType);
  }
  @override
  FlutterSmartNotifyStyle get notifyStyle => NotifyStyleFactory.create();
}

///网络配置（URL、超时、Header）
class MyNetWorkConfig implements NetWorkConfig {
  @override
  String get baseUrl => EnvManager.appBaseUrl;

  @override
  Map<String, String> get headers => {
    'Content-Type': 'application/json',
    'Accept': 'application/json',
    'X-Platform': AppPlatform.displayName,
    // 'User-Agent': AppPlatform.buildUserAgent(),
    // if (EnvManager.currentEnv == EnvType.dev) 'X-Debug': 'true',
  };

  @override
  Duration get connectTimeout => _getTimeout();

  @override
  Duration get receiveTimeout => _getTimeout();

  /// 根据环境配置超时时间
  Duration _getTimeout() {
    switch (EnvManager.currentEnv) {
      case EnvType.dev:
        return const Duration(seconds: 60);
      case EnvType.test:
        return const Duration(seconds: 45);
      case EnvType.prod:
        return const Duration(seconds: 30);
      default:
        return const Duration(seconds: 30);
    }
  }

  // 如有 creator 也要实现
  // @override
  // NetConfig<D,S> Function<D,S> get creator => ...;
}
